<?php

namespace App\Http\Controllers;

use App\Models\Activity;
use App\Models\ActivityProjectVideo;
use App\Models\Article;
use App\Models\Project;
use App\Models\Recommend;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\RecommendRequest;

class RecommendController extends CommonController
{
    /**
     * 获取列表
     */
    public function getList()
    {
        $lists = Recommend::orderBy('id', 'asc')->paginate(10);

        return view('recommend.list')->with('lists', $lists)->with("actions", $this->actions);
    }


    /**
     * 执行一个搜索
     */
    public function rSearch(Request $request)
    {
        $data = $request->input();
        $sql = Recommend::orderBy('id', 'asc');

        if (isset($data['subject']) && $data['subject'] != '') {
            $sql->where('subject', 'like', '%' . trim($data['subject']) . '%');
        }


        if (isset($data['position']) && $data['position'] != '') {
            $sql->where('position', $data['position']);
        }

        $lists = $sql->paginate(10);

        return view('recommend.search')->with("lists", $lists)->with("subject", trim($data['subject']))
            ->with("position", $data['position'])->with("actions", $this->actions);

    }




    public function postSearch(Request $request)
    {
        $data = $request->input();
//        dd($data);exit;
        if ($data['table'] != 'project' && $data['table'] != 'activity' && $data['table'] != 'article' && $data['table'] != 'video') {
            return AjaxCallbackMessage('你未选择所处于的表格', false);
        }

        if ($data['title'] == '') {
            return AjaxCallbackMessage('你未输入标题，无法进行模糊搜索', false);
        }

        if ($data['table'] == 'project') {
            $result = Project::where('subject', 'like', '%' . $data['title'] . '%')->get();
        } elseif ($data['table'] == 'activity') {
            $result = Activity::where('subject', 'like', '%' . $data['title'] . '%')->get();
        } elseif ($data['table'] == 'video') {
            $result = ActivityProjectVideo::where('subject', 'like', '%' . $data['title'] . '%')->get();
        }else {
            $result = Article::where('subject', 'like', '%' . $data['title'] . '%')->get();
        }
//        dd($result);exit;

        if (count($result)) {
            return AjaxCallbackMessage($result, true);
        } else {
            return AjaxCallbackMessage('该表格中没有与你输入的标题能够匹配的记录', false);
        }
    }

    /**
     * 获取创建页面
     */
    public function getCreate()
    {

        return view('recommend.create');
    }

    /**
     * 获取编辑页面
     */
    public function getEdit(Request $request)
    {
        $data = $request->input();
        $list = Recommend::where('id', $data['id'])->first();

        if ($list->table == 'project') {
            $recommend = Project::where('id', $list->post_id)->first();
        } elseif ($list->table == 'activity') {
            $recommend = Activity::where('id', $list->post_id)->first();
        }elseif ($list->table == 'video') {
            $recommend = ActivityProjectVideo::where('id', $list->post_id)->first();
        }else {
            $recommend = Article::where('id', $list->post_id)->first();
        }


        return view('recommend.edit')->with('list', $list)->with('recommend',$recommend);
    }

    /**
     * 执行一个创建
     */
    public function postCreate(RecommendRequest $request)
    {
        $data = $request->input();
        unset($data['_token'], $data['formType'],$data['title']);

        $result = Recommend::create($data);
        if (is_object($result)) {
            return AjaxCallbackMessage('创建成功，新推荐信息的id为' . $result->id, true, url('recommend/list'));
        } else {
            return AjaxCallbackMessage('创建失败', false);
        }
    }

    /**
     * 执行一个编辑
     */
    public function postEdit(RecommendRequest $request)
    {
        $data = $request->input();
        unset($data['_token'], $data['formType'], $data['title']);

        Recommend::where('id', $data['id'])->update($data);

        return AjaxCallbackMessage('编辑成功', true, url('recommend/list'));
    }

    /**
     * 删除一个推荐
     *
     */
    public function postDelete(Request $request)
    {
        $data = $request->input();
        Recommend::where('id', $data['id'])->delete();

        return AjaxCallbackMessage('删除成功', true, url('recommend/list'));
    }
}
